package org.example.Swing.xiangmu.login;

import org.example.Swing.handler.LoginHandler;

import javax.swing.*;
import java.awt.*;

public class LoginView extends JFrame {
    JLabel nameLabel = new JLabel("学生成绩管理系统", JLabel.CENTER);


    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("用户名：");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码：");
    JPasswordField pwdField = new JPasswordField();
    JButton loginBtn = new JButton("登录");
    JButton zhuceBtn = new JButton("注册");


    SystemTray systemTray;
    TrayIcon trayIcon;
    LoginHandler loginHandler;

    public LoginView() {
        super("学生成绩管理系统");
        loginHandler = new LoginHandler(this);
        Container contenPane = getContentPane();


        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 80));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));

        loginBtn.setFont(centerFont);
        zhuceBtn.setFont(centerFont);
        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        loginBtn.addActionListener(loginHandler);
        centerPanel.add(loginBtn);
        zhuceBtn.addActionListener(loginHandler);
        centerPanel.add(zhuceBtn);
        //弹簧布局
        //布局userNameLabel
        layoutCenter();

        contenPane.add(nameLabel, BorderLayout.NORTH);
        contenPane.add(centerPanel, BorderLayout.CENTER);

        //设置loginBtn为默认按钮
        getRootPane().setDefaultButton(loginBtn);
        LoginView.class.getClassLoader().getResource("");
        setIconImage(null);
        setSize(700, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

    }

    private void layoutCenter() {
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)), Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);


        //userLabel
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
        //pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userNameLabel);
        //pwdField
        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);
        //loginBtn
        springLayout.putConstraint(SpringLayout.WEST, loginBtn, 50, SpringLayout.WEST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 20, SpringLayout.SOUTH, pwdLabel);
        //zhuceBtn
        springLayout.putConstraint(SpringLayout.WEST, zhuceBtn, 80, SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, zhuceBtn, 0, SpringLayout.NORTH, loginBtn);
    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }
}
